package com.yyg.wiki.service;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyg.wiki.domain.Category;
import com.yyg.wiki.domain.CategoryExample;
import com.yyg.wiki.mapper.CategoryMapper;
import com.yyg.wiki.req.CategoryQueryReq;
import com.yyg.wiki.req.CategorySaveReq;
import com.yyg.wiki.resp.CategoryQueryResp;
import com.yyg.wiki.resp.PageResp;
import com.yyg.wiki.util.CopyUtil;
import com.yyg.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: CategoryService
 * Description:
 * date: 2021/7/22 15:00
 *
 * @author 17878
 * @since JDK 1.8
 */
@Service
public class CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;
    private static final Logger LOG = LoggerFactory.getLogger(CategoryService.class);
    public PageResp<CategoryQueryResp> list(CategoryQueryReq Req){
        PageHelper.startPage(Req.getPage(),Req.getSize());
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());
//        List<CategoryResp> respList=new ArrayList<>();
//        for (Category category : categoryList) {
//
//            CategoryResp categoryResp = CopyUtil.copy(category, CategoryResp.class);
//            respList.add(categoryResp);
//        }
        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);
        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    public List<CategoryQueryResp> all(){
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);

        return list;
    }

    /**
     *
     * 功能描述:新增操作
     *
     * @param:
     * @return:
     * @auther: 17878
     * @date: 2021/7/29 15:14
     */
    public void save(CategorySaveReq req){
        System.out.println("genggaikkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"+req);
        Category category=CopyUtil.copy(req,Category.class);
        if (ObjectUtils.isEmpty(req.getId())){
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        }else {
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }
}
